package com.share.chat.controller;

import com.share.chat.domain.ResultInfo;
import com.share.chat.domain.User;
import com.share.chat.service.UserService;
import com.share.chat.vo.LoginParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 谢佳豪
 * @data 2021/7/3 - 18:01
 * @description
 */
@Controller
@ResponseBody
@RequestMapping("/user")
public class UserController {

    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }


    @GetMapping("/repeatUsername/{username}")
    public ResultInfo repeatUsername(@PathVariable String username) {
        return userService.repeatUsername(username);
    }

    @GetMapping("/repeatTelephone/{telephone}")
    public ResultInfo repeatTelephone(@PathVariable String telephone) {
        return userService.repeatTelephone(telephone);
    }

    @GetMapping("/repeatEmail")
    public ResultInfo repeatEmail(@RequestParam String email) {
        return userService.repeatEmail(email);
    }

    @PostMapping("/login")
    public ResultInfo login(@RequestBody LoginParam param, HttpSession session, HttpServletRequest request, HttpServletResponse response) {

        //对表单的密码进行加密
        //String md5Password = DigestUtils.md5DigestAsHex(param.getPassword().getBytes());

        //查询数据，比较是否登录成功
        User user = userService.login(param.getAccount(), param.getPassword());

        if (user != null && user.getUsername() != null) {
            //修改用户的登录状态
            user.setOnline(true);
            userService.updateOnlineState(user.getUsername(), true);

            //session存放user信息
            session.setAttribute("user", user);

            return ResultInfo.successInfo("登录成功");
        }
        return ResultInfo.failInfo("请检查用户名和密码");
    }

    @GetMapping("/isLogin")
    public ResultInfo checkJsessionid(HttpSession session) {

        User user = (User) session.getAttribute("user");

        //检验是否存在user值来判断是否以及登录
        if (user != null) {

            //将已登录的用户的头像和用户名返回
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("userPicture", user.getPicture());
            resultMap.put("username", user.getUsername());
            return ResultInfo.successInfo("已登录！", resultMap);

        } else {
            return ResultInfo.failInfo("请先登录！");
        }

    }


    @PostMapping("/registration")
    public ResultInfo registration(@RequestBody User user) {
        return userService.registration(user);
    }

    @GetMapping("/logout")
    public ResultInfo logout(HttpSession session) {
        //修改用户的登录状态
        User user = (User) session.getAttribute("user");
        userService.updateOnlineState(user.getUsername(), false);

        //清空session域中的user
        session.invalidate();

        return ResultInfo.successInfo("安全退出");

    }

}
